import svn.fs, svn.repos, svn.core


class Pool:
    def __init__(self, handle):
        self.handle = handle

    def subpool(self):
        p = svn.core.svn_pool_create(self.handle)
        child = Pool(p)
        child.parent = self
        return child

    def __del__(self):
        if (hasattr(self, 'parent')):
            print "destroying subpool"
            svn.core.svn_pool_destroy(self.handle)

def File:
    def __init__(self, name, node, pool):
        self.pool = pool
        self.name = name
        self.node = node
    def is_valid(self):
        return hasattr(self.node, '_is_valid')
    

def Root:
    def __init__(self, fs, rev, pool):
        self.pool = pool.subpool()
        self.root = svn.fs.svn_fs_revision_root(fs, rev, self.pool.handle)

    def isdir(self, path):
        return svn.fs.svn_fs_is_dir(self.root, path, self.pool.handle)

    def listdir(self, path):
        

def Repository:
    def __init__(self, path, callback):
        self.callback = callback
        svn.core.run_app(self._callback, path)

    def _callback(self, pool, path):
        self.pool = Pool(pool)
        repos = svn.repos.svn_repos_open(path, self.Pool.handle)
        self.fs = svn.repos.svn_repos_fs(self)
        self.set_rev()
        self.callback(self)

    def set_rev(self, version = None):
        if version is None:
            version = self.latest_rev()
        self.root = Root(self.fs, version, self.pool)

    def latest_rev(self):
        return svn.fs.svn_fs_youngest_rev(self.fs, self.pool.handle)

